package com.klouda.gboff.db;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.klouda.gboff.BookActivity;
import com.klouda.gboff.MainActivity;

public class GbOffDbHandler {

  // If you change the database schema, you must increment the database version.
  public static final int DATABASE_VERSION = 3;
  public static final String DATABASE_NAME = "gboff.db";

  public GbOffDbHandler(MainActivity p_mainActivity) {
    m_activity = p_mainActivity;
  }
  
  public GbOffDbHandler open() {
    m_dbHelper = new PlatakyDbHelper(m_activity);
    m_db = m_dbHelper.getWritableDatabase();
    return this;
  }

  public void close() {
    if (m_dbHelper != null) {
      m_dbHelper.close();
    }
  }
  
  public boolean createBook(String p_id, String p_name, String p_authors, byte[] p_thumbnail) {
    ContentValues l_cvBook = new ContentValues();
    l_cvBook.put(Tables.TBooks.COLUMN_NAME_ID, p_id);
    l_cvBook.put(Tables.TBooks.COLUMN_NAME_NAME, p_name);
    l_cvBook.put(Tables.TBooks.COLUMN_NAME_AUTHORS, p_authors);
    l_cvBook.put(Tables.TBooks.COLUMN_NAME_THUMBNAIL, p_thumbnail);
    try {
      open();
      return (m_db.insertOrThrow(Tables.TBooks.TABLE_NAME, null, l_cvBook) < 0 ? false : true);      
    } catch (Exception l_ex) {
      doHandleException(l_ex);
      return false;
    } finally {
       close();
    }
  }

  public boolean deleteAll() {
    try {
      open();
      //m_db.delete(Tables.TBooks.TABLE_NAME, Tables.TBooks.COLUMN_NAME_ID + "=?", new String[]{String.valueOf(p_id)});
      m_db.delete(Tables.TBooks.TABLE_NAME, null, null);
    } catch (Exception l_ex) {
      doHandleException(l_ex);
      return false;
    } finally {
       close();
    }
    return true;
  }
  
  public Cursor getAllBooks() {
    return m_db.query(Tables.TBooks.TABLE_NAME, new String[] {
        Tables.TBooks.COLUMN_NAME_ID, Tables.TBooks.COLUMN_NAME_NAME,
        Tables.TBooks.COLUMN_NAME_AUTHORS, Tables.TBooks.COLUMN_NAME_THUMBNAIL }, null, null, null, null, 
        Tables.TBooks.COLUMN_NAME_NAME  + " COLLATE LOCALIZED ASC");
  }
  
  //!td predelat exception handling? udelat neco jako hlavni logger, jde logovat do fajlu?
  private void doHandleException(Exception p_ex) {
    Log.e(BookActivity.ERROR_TAG, "Error while working with database", p_ex);
    if (m_activity != null) {
      m_activity.showErrorDialog(p_ex);
    }
  }  
  
  private final MainActivity m_activity;
  private PlatakyDbHelper m_dbHelper = null;// new PlatakyDbHandler(this);
  private SQLiteDatabase m_db = null;//l_dbHelper.getWritableDatabase();  
  
  
  private static final String TEXT_TYPE = " TEXT";
  @SuppressWarnings("unused")
  private static final String REAL_TYPE = " REAL";
  @SuppressWarnings("unused")  
  private static final String INTEGER_TYPE = " INTEGER";
  private static final String BLOB_TYPE = " BLOB";
  
  private static final String COMMA_SEP = ",";
  private static final String NOT_NULL = " NOT NULL";
  @SuppressWarnings("unused")  
  private static final String AUTOINCREMENT = " AUTOINCREMENT";
  private static final String PRIMARY_KEY = " PRIMARY KEY";
  @SuppressWarnings("unused")  
  private static final String DEFAULT = " DEFAULT ";

  private static final String SQL_CREATE_TABLES = "CREATE TABLE "
      + Tables.TBooks.TABLE_NAME + " (" + Tables.TBooks.COLUMN_NAME_ID
      + TEXT_TYPE + PRIMARY_KEY + COMMA_SEP
      + Tables.TBooks.COLUMN_NAME_NAME + TEXT_TYPE + NOT_NULL + COMMA_SEP
      + Tables.TBooks.COLUMN_NAME_AUTHORS + TEXT_TYPE + COMMA_SEP
      + Tables.TBooks.COLUMN_NAME_THUMBNAIL + BLOB_TYPE + " )";

  private static final String SQL_DELETE_TABLES = "DROP TABLE IF EXISTS "
      + Tables.TBooks.TABLE_NAME;

  private static class PlatakyDbHelper extends SQLiteOpenHelper {
    public PlatakyDbHelper(Context context) {
      // ? !td ulozit context do membra?
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase p_db) {
      p_db.execSQL(SQL_CREATE_TABLES);

    }

    @Override
    public void onUpgrade(SQLiteDatabase p_db, int arg1, int arg2) {
      p_db.execSQL(SQL_DELETE_TABLES);
      onCreate(p_db);
    }
  }
  
  
}
